package animation
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;

	public class AnimationSys extends Sprite
	{
		public var dataList:Object = new Object();
		
		private var animationList:Object = new Object();
		private var m_EffectList:Array = new Array();
		
		private static var instance:AnimationSys;
		
		private var isRun:Boolean;
		
		public function AnimationSys()
		{
			
		}
		
		public static function get inst():AnimationSys
		{
			return instance ||= new AnimationSys();
		}
		
		public function addAnimationData(strKey:String, data:AnimationData):void
		{
			dataList[strKey] = data;
		}
		
		public function getAnimationData(strKey:String):AnimationData
		{
			return dataList[strKey] as AnimationData;
		}
		
		public function addAnimation(key:*, an:IAnimation):void
		{
			animationList[key] = an;
		}
		
		public function removeAnimation(key:*):void
		{
			animationList[key] = null;
			delete animationList[key];
		}
		
		public function removeAnimationByObj(obj:IAnimation):void
		{
			for (var key:IAnimation in animationList)
			{
				var data:IAnimation = animationList[key];
				if(obj == data)
				{
					data = null;
					delete animationList[key];
				}
			}
		}
		
		public function addEffect(obj:IAnimation):void
		{
			m_EffectList.push(obj);
		}
		
		public function removeEffect(obj:IAnimation):void
		{
			var i:int = m_EffectList.indexOf(obj);
			
			m_EffectList.splice(i, 1);
		}

		public function on():void
		{
			if(run)
				return;
			isRun = true;
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		public function off():void
		{
			if(!run)
				return;
			isRun = false;
			this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		protected function onEnterFrame(event:Event):void
		{
			for each(var obj:IAnimation in animationList)
			{
				obj && obj.updata();
			}
			
			for(var i:int=0; i<m_EffectList.length; i++)
			{
				var effect:IAnimation = m_EffectList[i];

				effect && effect.updata();

			}
		}
		
		public function get run():Boolean
		{
			return isRun;
		}
		
		
		/**
		 * 水平翻转图像
		 */
		private function getMirror(bd:BitmapData):BitmapData 
		{
			var bmd:BitmapData = new BitmapData(bd.width, bd.height, true, 0x00000000);
			var matrix:Matrix = new Matrix();
			
			matrix.a = -1;
			matrix.tx = bd.width;
			bmd.draw(bd,matrix);
			
			return bmd;
		}

	}
}